package com.ruoyi.seal.service.impl;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.SequenceUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.seal.domain.Seal;
import com.ruoyi.seal.domain.SealType;
import com.ruoyi.seal.mapper.SealMapper;
import com.ruoyi.seal.service.ISealService;
import com.ruoyi.system.service.ISysCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SealServiceImpl implements ISealService {
    @Autowired
    private SealMapper sealMapper;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    public RedisTemplate redisTemplate;

    @Autowired
    public ISysCommonService sysCommonService;

    @Override
    public Seal selectSealBySealId(String sealId, Long userId) {
        return sealMapper.selectSealBySealIdAndUid(sealId, userId);
    }

    @Override
    public Seal selectSealBySealIdAndApproveId(Seal seal) {
        return sealMapper.selectSealBySealIdAndApproveId(seal);
    }

    /**
     * 查询自身用印列表
     *
     * @param seal 用印
     * @return 用印
     */
    @Override
    public List<Seal> selectSealList(Seal seal) {
        return sealMapper.selectSealList(seal);
    }

    /**
     * 科室负责人查询用印列表
     *
     * @param seal 用印
     * @return 用印集合
     */
    @Override
    public List<Seal> selectSealListByDept(Seal seal) {
        return sealMapper.selectSealListByDept(seal);
    }

    /**
     * 指定审批人查询用印列表
     *
     * @param seal 用印
     * @return 用印集合
     */
    @Override
    public List<Seal> selectSealListByMaster(Seal seal) {
        return sealMapper.selectSealListByMaster(seal);
    }

    /**
     * 科室审批
     *
     * @param seal 用印
     * @return 结果
     */
    @Override
    public int deptApprovalSeal(Seal seal) {
        if (seal.getDeptIdea() != null) {
            //不同意
            if (seal.getDeptIdea().equals("2")) {
                //退回修改
                seal.setDeleted(4);
            }
            //同意
            if (seal.getDeptIdea().equals("1")) {
                seal.setDeleted(1);
            }
        }
        return sealMapper.deptApprovalSeal(seal);
    }

    /**
     * 指定审批人审批
     *
     * @param seal 用印
     * @return 结果
     */
    @Override
    public int masterApproval(Seal seal) {
        if (seal.getSealIdea() != null) {
            //不同意
            if (seal.getSealIdea().equals("2")) {
                //退回修改
                seal.setDeleted(4);
            }
            //同意
            if (seal.getSealIdea().equals("1")) {
                seal.setDeleted(2);
            }
        }
        return sealMapper.masterApproval(seal);
    }

    /**
     * 新增用印
     *
     * @param seal 用印
     */
    @Override
    public int insertSeal(Seal seal) {
        //流水号自增ID设置

        seal.setSealId(IdUtils.serialNumber("Y", redisCache, ISealService.class));
        return sealMapper.insertSeal(seal);
    }

    /**
     * 查询用印类型
     *
     * @return 用印类型集合
     */
    @Override
    public List<SealType> querySealType() {
        return sealMapper.queerSealTypes();
    }

    /**
     * 批量删除用印
     *
     * @param sealIds 需要删除的用印主键
     * @return 结果
     */
    @Override
    public int deleteSealBySealIds(String[] sealIds, Long userId) {
        return sealMapper.deleteSealBySealIds(sealIds, userId);
    }

    @Override
    public String getDeptName(Long deptId) {
        return sealMapper.getDeptName(deptId);
    }

    @Override
    public String getUserName(Long userId) {
        return sealMapper.getUserName(userId);
    }


    /**
     * 判断选中的所要导出的ids是否属于自己
     *
     * @param ids
     * @param userId
     * @return boolean
     */
    @Override
    public boolean isSelfSealList(List<String> ids, Long userId) {
        Seal seal = new Seal();
        seal.setUserId(userId);
        List<Seal> list = selectSealList(seal);
        List<String> allIdsList = new ArrayList<>();
        //取自身所有用印id放入List<String>
        for (Seal allIds : list) {
            allIdsList.add(allIds.getSealId());
        }
        boolean resultBoolean = true;
        for (String s : ids) {
            boolean contains = allIdsList.contains(s);
            //如果有一个不包含在list COPY_SERVICE中，则为false
            if (!contains) {
                resultBoolean = false;
            }
        }
        return resultBoolean;
    }

    /**
     * 判断seal是否属于自己
     *
     * @param sealId
     * @param userId
     * @return int
     */
    @Override
    public int isSelfSeal(String sealId, Long userId) {
        return sealMapper.isSelfSeal(sealId, userId);
    }

    /**
     * 通过List<String> ids获取List<Seal>
     */
    @Override
    public List<Seal> getListSealByIds(List<String> ids) {
        return sealMapper.getListSealByIds(ids);
    }

    @Override
    public int selectDeptToBeApprovedTask() {
        int count = 0;
        //其他身份，判断是否拥有访问科室用印审批模块的权限
        Set<String> permissions = SecurityUtils.getLoginUser().getPermissions();
        boolean flag = permissions.contains("seal:dept:approval");
        if (flag) {
            count = sealMapper.selectDeptSealCountByUserId(SecurityUtils.getUserId());
        }
        return count;
    }

    @Override
    public int selectMasterToBeApprovedTask() {
        int count = 0;
        //其他身份，判断是否拥有访问科室用印审批模块的权限
        Set<String> permissions = SecurityUtils.getLoginUser().getPermissions();
        boolean flag = permissions.contains("seal:master:approval");
        if (flag) {
            count = sealMapper.selectMasterSealCountByUserId(SecurityUtils.getUserId());
        }
        return count;
    }

    @Override
    public int updateSeal(Seal seal) {

        return sealMapper.updateSeal(seal);
    }

    @Override
    public void intoApprovel(Seal seal) {
        if (selectSealBySealIdAndApproveId(seal).getDeleted() == 0) {
            sealMapper.intoApprovel(seal);
        }
    }

    public void finshApprovel(Seal seal) {
        if (selectSealBySealIdAndApproveId(seal).getDeleted() == 1) {
            sealMapper.finshApprovel(seal);
        }
    }

    /**
     * 个人补传附件
     * 只有审核流程通过后才可以
     */
    @Override
    public int reUpFile(Seal seal) {
        return sealMapper.reUpFile(seal);
    }
}
